/*
 * To change primaryStage template, choose Tools | Templates and open the
 * template in the editor.
 */
package goodenglish.interfaces;

import goodenglish.infrastructure.Config;
import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.TimelineBuilder;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.DepthTest;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 * 程序的主界面类
 * 
 * @author 关攀攀 panpan_001@126.com
 */
public class Main extends Application {

	public static BorderPane root;
	public static Stage stage;
	public static Main instance;
	private StackPane modalDimmer;

	@Override
	public void start(Stage primaryStage) {
		// 初始化系统配置，原来在main函数中，后因打包后start先于main执行
		Config.init();

		instance = this;
		stage = primaryStage;
		primaryStage.setTitle(Config.APP_TITLE);

		StackPane layerPane = new StackPane();
		root = new BorderPane();
		// root.getStyleClass().add("application");
		root.setId("root");
		layerPane.setDepthTest(DepthTest.DISABLE);
		layerPane.getChildren().add(root);
		// create modal dimmer, to dim screen when showing modal dialogs
		modalDimmer = new StackPane();
		modalDimmer.setId("ModalDimmer");
		modalDimmer.setOnMouseClicked(new EventHandler<MouseEvent>() {
			public void handle(MouseEvent t) {
				t.consume();
				hideModalMessage();
			}
		});
		modalDimmer.setVisible(false);
		layerPane.getChildren().add(modalDimmer);
		Scene scene = new Scene(layerPane, Config.SCREEN_WIDTH, Config.SCREEN_HEIGHT, Color.WHITESMOKE);
		primaryStage.setScene(scene);
		primaryStage.centerOnScreen();

		// 加载css文件
		scene.getStylesheets().addAll(Main.class.getResource("goodenglish.css").toExternalForm(),
		// Workaround for CSS issue with HTML Editor
				com.sun.javafx.scene.web.skin.HTMLEditorSkin.class.getResource("html-editor.css").toExternalForm());
		root.setTop(new MenuToolBar());
		root.setCenter(new IndexPane());
		primaryStage.show();

	}

	/**
	 * Show the given node as a floating dialog over the whole application, with
	 * the rest of the application dimmed out and blocked from mouse events.
	 * 
	 * @param message
	 */
	public void showModalMessage(Node message) {
		modalDimmer.getChildren().add(message);
		modalDimmer.setOpacity(0);
		modalDimmer.setVisible(true);
		modalDimmer.setCache(true);
		TimelineBuilder.create().keyFrames(new KeyFrame(Duration.seconds(1), new EventHandler<ActionEvent>() {
			public void handle(ActionEvent t) {
				modalDimmer.setCache(false);
			}
		}, new KeyValue(modalDimmer.opacityProperty(), 1, Interpolator.EASE_BOTH))).build().play();
	}

	/**
	 * Hide any modal message that is shown
	 */
	public void hideModalMessage() {
		modalDimmer.setCache(true);
		TimelineBuilder.create().keyFrames(new KeyFrame(Duration.seconds(1), new EventHandler<ActionEvent>() {
			public void handle(ActionEvent t) {
				modalDimmer.setCache(false);
				modalDimmer.setVisible(false);
				modalDimmer.getChildren().clear();
			}
		}, new KeyValue(modalDimmer.opacityProperty(), 0, Interpolator.EASE_BOTH))).build().play();
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String[] args) {

		// 启动主界面程序
		Application.launch(args);
	}
}
